package com.amentretenimiento;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Dictionary;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

//import com.smartthinking.CICPNL.home.objects.clsErrorInternet;

public class saxXML {
	private URL rssUrl;
 

	public saxXML(String url ) {
		try {
			
			
			this.rssUrl = new URL(url);
			
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	public ArrayList<Dictionary<String, String>> parse(int flag) {
		SAXParserFactory factory = SAXParserFactory.newInstance();

		try {
			if(flag == 0){
			SAXParser parser = factory.newSAXParser();
			handXML handler = new handXML();
			parser.parse(this.getInputStream(), handler);

			return handler.getXML();
			}
			
			else if(flag == 1){
				SAXParser parser = factory.newSAXParser();
				handXMLResults handler = new handXMLResults();
				parser.parse(this.getInputStream(), handler);

				return handler.getXML();
			}else if (flag == 2){
				SAXParser parser = factory.newSAXParser();
				handXMLRows handler = new handXMLRows();
				parser.parse(this.getInputStream(), handler);

				return handler.getXML();
			}
			return null;
		} catch (Exception e) {
			 return null;
			
		}
	}

	private InputStream getInputStream() {
		try {
		
			URLConnection connection = rssUrl.openConnection();
			connection.setConnectTimeout(10000);
			return connection.getInputStream();

		} catch (IOException e) {
			
			
			 e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
